package programming.in.scala

/**
  * Created by shichen on 2017/7/6.
  */

object Email {
  //The injection method (optional)
  def apply(user: String, domain: String) = user + "@" + domain

  //The extraction method (mandatory)
  def unapply(str: String): Option[(String,String)] = {
    val parts = str.split("@")
    if (parts.length == 2) Some(parts(0),parts(1)) else None
  }
}
object ch26_extractors {
  val selectorString = "merryok@qq.com"
  selectorString match {
    case Email(user,domain) => //would lead to call Email.unapply(selectorString)
  }

  val x:Any = 1 //如果不是String类型，匹配失败
  x match {
    case Email(user,domain) =>
  }
}
